package com.wyh.utils.g3utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/05/28/16:42
 **/
public class BillidGenerateUtils {

    private static final AtomicInteger COUNTER = new AtomicInteger(0);

    /**
     * Generates a 18-digit unique index.
     *
     * @return An 18-digit string representing a unique index.
     */
    public static String generateFixedLengthIndex() {
        long currentTimeMillis = System.currentTimeMillis();
        int randomPart = new Random().nextInt(9999); // 生成0到9999之间的随机数
        int counterPart = COUNTER.incrementAndGet(); // 自增计数器，确保唯一性

        // 拼接各个部分并转换为18位字符串
        String timePart = String.format("%013d", currentTimeMillis);
        String randomPartStr = String.format("%04d", randomPart);
        String counterPartStr = String.format("%02d", counterPart);

        return "9"+generateTimestampWithMilliseconds();
    }
    public static String generateTimestampWithMilliseconds() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        return now.format(formatter);
    }
}
